package org.wikinews.en.wikinewsreader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Article extends Activity implements Runnable{
	// This activity loosely based on the example code from:
	// http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
	private static final String TAG = "WikinewsReader" ;
	private int STORYID;
	private String[] storyRay;
	private ProgressDialog progDialog = null;
	private String finalText = null;
	private Context ctx;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.article);
    	Log.d(TAG, "Article.onCreate()");
    	ctx = this;
	}

	@Override
    public void onResume() {
		super.onResume();
    	if(finalText == null){
    		//First time we call the activity, we'll need to do the standard text
    		Log.d(TAG, "finalText null");
	    	storyRay = getIntent().getStringArrayExtra("all");
	    	STORYID = getIntent().getIntExtra("id", 0);
	        Log.d(TAG, "Article ID " + STORYID);
	
	        //this.setTitle(storyRay[0]); //Conceptually good, but title too long for window.
	        progDialog = ProgressDialog.show(Article.this,"Please wait...", "Getting story...", true);
	
	    	Thread thread = new Thread((Runnable) Article.this);
	    	thread.start();
	        Log.d(TAG, "Article.thread.Start()");
    	}else{
    		//We'll end up here if finalText has been pre-populated by onRestoreInstanceState
    		Log.d(TAG, "finalText containts data");
    		final TextView arty = (TextView) findViewById(R.id.article_content);
        	arty.setText(finalText);
    	}
	}
	
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
    	Log.d(TAG,"onRestoreInstanceState()");
    	finalText = savedInstanceState.getString("finalText");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    	Log.d(TAG,"onSaveInstanceState()");
    	// Instead of storing multiple different values, we're just going to
    	// store the entire article box text
    	final TextView arty = (TextView) findViewById(R.id.article_content);
    	outState.putString("finalText", arty.getText().toString());
    }
    
    /*
     * Puts the menus up on screen programtically (non-Javadoc)
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    menu.add(0, R.id.launchweb, 0, getResources().getString(R.string.launchweb_label))
	    	.setIcon(R.drawable.ic_menu_view);
	    
	    return true;
    }

    /*
     * Make the menu options do something(non-Javadoc)
     * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
		    case R.id.launchweb: 
		        Uri uri = Uri.parse(WikinewsReader.getBaseURL()+storyRay[1]);
		        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		        startActivity(intent);
	    }
	    return false;
    }

	public void run() {
		finalText = getArticle();
		handler.sendEmptyMessage(0);
	}

    private String getArticle(){
    	String res = null;
    	Log.d(TAG, "Article.getArticle()");
    	try{
	        //STORYID = getIntent().getIntExtra("id", 0);
	        //storyRay = getIntent().getStringArrayExtra("all");
	        
	    	if(Prefs.getDownloadondemand(getBaseContext())){
	        	//DOD On - Need to download article text 
	    		Log.d(TAG, "getArticle - getDOD is true");
	        	String[][] sending = {{"mode","textonly"},{"id",Integer.toString(STORYID)}};
				res = NetTrick.doIt(sending, handler);
	        }else{
	        	//DOD Off - already got the article text from intent
	        	if(storyRay[2] == null){
	        		//BUT, DOD Could have JUST been turned off, leaving us w/o data
	        		//This catches that and Downloads anyways.
	        		Log.d(TAG, "getArticle - getDOD is false but storyRay text is null");
	        		String[][] sending = {{"mode","textonly"},{"id",Integer.toString(STORYID)}};
	        		res = NetTrick.doIt(sending, handler);
	        	}else{
	        		Log.d(TAG, "getArticle - getDOD is false");
	        		res = storyRay[2];
	        	}
	        }
        } catch (Exception e) {
        	Log.e(TAG,"General Exception");
        	Log.e(TAG, e.getMessage().toString());
        	handler.sendEmptyMessage(10);
        }
        return res;
    }
    
    private Handler handler = new Handler() {
		public void handleMessage(Message msg){
    		Log.d(TAG, "Article.handler.messageHandler()");
        	if(msg.what > 10){
        		WikinewsReader.generalError(msg.what, ctx);
        	}else{
	        	final TextView arty = (TextView) findViewById(R.id.article_content);
	        	arty.setText(storyRay[0] + "\n=-=-=-=-=-=-=\n"+finalText);
	        	progDialog.dismiss();
        	}
		}
    };
    
    
}
